﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace _070.EFCore一对多
{
	public class Program
	{
		static async Task Main(string[] args)
		{
			await using MyDbContext ctx = new MyDbContext();

			// 插入数据
			//Article a1 = new Article();
			//a1.Title = "微软发布.NET 6大版本的首个预览";
			//a1.Content = "微xxxx";
			//Comment c1 = new Comment() { Message = "支持" };
			//Comment c2 = new Comment() { Message = "微软太牛了" };
			//Comment c3 = new Comment() { Message = "火钳刘明" };
			//a1.Comments.Add(c1);
			//a1.Comments.Add(c2);
			//a1.Comments.Add(c3);

			//ctx.Articles.Add(a1);

			//await ctx.SaveChangesAsync();

			// 获取数据
			// Include() 指定多表的数据源
			/*Article a = ctx.Articles.Include(a => a.Comments).Single(a => a.Id == 1);
			Console.WriteLine(a.Id);
			Console.WriteLine(a.Title);
			foreach (Comment c in a.Comments)
			{
				Console.WriteLine(c.Id + ":" + c.Message);
			}*/

			var comment = ctx.Comments.Single(c => c.Id == 3);
			Console.WriteLine(comment.Id+","+comment.ArticleId);
		}
	}
}
